<template>
  <!--
    学生 功能列表

    @author alinesno ${authorEmail}
    @date 2021-09-07 21:55:30
  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentNumber">
        <el-input
          v-model="queryParams.studentNumber"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" ref="queryParams.collegeId" size="small">
          <el-option v-for="item in LearnCollegeList"
            :key="item.id"
            :label="item.collegeName"
            :value="item.id"
          ></el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.collegeId"-->
<!--          placeholder="请选择学院"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" ref="queryParams.classId" size="small">
          <el-option v-for="item in LearnClassList"
                     :key="item.id"
                     :label="item.className"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入学时间" prop="admission">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.admission"
          range="admission"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入学时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LearnStudentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="left" prop="studentNumber" />
      <el-table-column label="姓名" align="left" prop="studentName" />
      <el-table-column label="学院" align="left" prop="collegeId">
        <template slot-scope="scope">
          <span>{{getLearnCollegeName(scope.row.collegeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="left" prop="classId">
        <template slot-scope="scope">
          <span>{{getLearnClassName(scope.row.classId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入学时间" align="left" prop="admission" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.admission, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="状态" prop="hasStatus">-->
<!--        <template slot-scope="scope">-->
<!--        <el-switch-->
<!--            v-model="scope.row.hasStatus"-->
<!--            :active-value="0"-->
<!--            :inactive-value="1"-->
<!--            @change="handleStatusChange(scope.row)"-->
<!--          ></el-switch>-->
<!--         </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="添加时间" align="center" prop="addTime">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseDatetime(scope.row.addTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button

            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentNumber">
          <el-input v-model="form.studentNumber" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学院" prop="collegeId">
<!--          <el-input v-model="form.collegeId" placeholder="请选择学院" />-->
          <el-select v-model="form.collegeId" ref="form.collegeId"  size="small">
            <el-option v-for="item in LearnCollegeList"
                       :key="item.id"
                       :label="item.collegeName"
                       :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" ref="form.classId" size="small">
            <el-option v-for="item in LearnClassList"
                       :key="item.id"
                       :label="item.className"
                       :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入学时间" prop="admission">
          <el-date-picker clearable size="small"
            v-model="form.admission"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入学时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLearnStudent,
  getLearnStudent,
  delLearnStudent,
  addLearnStudent,
  changeLearnStudentField,
  changeStatusLearnStudent,
  updateLearnStudent,
  exportLearnStudent } from "@/api/student/LearnStudent";
import  Condition  from "alinesno-ui/packages/Search/Condition";
import {
  listLearnCollege } from "@/api/student/LearnCollege";

import {
  listLearnClass } from "@/api/student/LearnClass";
import {searchParam} from "alinesno-ui/packages/Search";

export default {
  name: "LearnStudent",
  components: {
  },
  data() {
    return {
      //下拉菜单班级列表
      LearnClassList:[],
      //查询班级参数
      searchClassParams:[],
      //下拉菜单学院列表
      LearnCollegeList:[],
      //查询学院参数
      searchCollegeParams:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态
      statusOptions:[] ,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      LearnStudentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentNumber: null,
        studentName: null,
        collegeId: null,
        classId: null,
        admission: null
      },
      // 查询参数配置对象
      queryParamsConfig: {
        studentNumber: Condition.like(),
        studentName: Condition.like(),
        collegeId: Condition.like(),
        classId: Condition.like(),
        admission: Condition.like(),
      },
      // 搜索参数
      searchParams:[] ,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentNumber: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        studentName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        collegeId: [
          { required: true, message: "学院不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        admission: [
          { required: true, message: "入学时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.searchClassParams = this.searchParam(null, {pageSize: 10});
    this.searchCollegeParams = this.searchParam(null, {pageSize: 10});
    this.handleQuery() //解决刷新界面不显示内容
    // 查询公共状态
    this.getDicts("has_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true;

      listLearnClass(this.searchClassParams).then(response => {
        this.LearnClassList = response.rows;
      });

      listLearnCollege(this.searchCollegeParams).then(response => {
        this.LearnCollegeList = response.rows;
      });
      listLearnStudent(this.searchParams).then(response => {
        this.LearnStudentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentNumber: null,
        studentName: null,
        collegeId: null,
        classId: null,
        admission: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 获取参数
      this.searchParams = this.searchParam(this.queryParamsConfig, this.queryParams);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentNumber = row.id || this.ids
      getLearnStudent(studentNumber).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLearnStudent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLearnStudent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentNumbers = row.id || this.ids;
      this.$confirm('是否确认删除学生编号为"' + studentNumbers + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLearnStudent(studentNumbers);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 状态修改 **/
    handleStatusChange(row) {
      return changeStatusLearnStudent(row.id, row.status).then(response=>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      });
    },
    /** 修改字段状态 **/
    chanageFile(value , filed , id){
      return changeLearnStudentField(value , filed , id).then(response =>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      }) ;
    } ,
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学生数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLearnStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    getLearnCollegeName(id) {
      //console.log(this.LearnCollegeList);
      for(let i = 0;i < this.LearnCollegeList.length;i++) {
        //console.log(this.LearnCollegeList[i].id===id)
        if(this.LearnCollegeList[i].id.toString()===id) {
          return this.LearnCollegeList[i].collegeName;
        }
      }
      return "";
    },
    getLearnClassName(id) {
      for(let i = 0;i < this.LearnClassList.length;i++) {
        if(this.LearnClassList[i].id.toString()===id) {
          return this.LearnClassList[i].className;
        }
      }
      return "";
    }
  }
};
</script>
